<?php

function list_komponen_gaji($jid){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');
	
	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Nama'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  
		'Model Entri'=>array('style'=>'text-align:left;border-bottom:2px solid;width:20%'),  
	 	'Jurnal Gaji'=>array('style'=>'text-align:left;border-bottom:2px solid;width:30%'),  				
	 	'Pajak'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  				
		' '=>array('style'=>'border-bottom:2px solid;width:10%')
		);
	$com = isset($_GET['com']) ? $_GET['com']: '';
	$task = isset($_GET['task']) ? $_GET['task']: '';
	$field = isset($_GET['field']) ? $_GET['field']: '';
	$key = isset($_GET['key']) ? $_GET['key']: '';
	$facebox=true;
	$query = "SELECT * FROM pr_komponen_gaji WHERE tipe=".$jid." ORDER BY komponen_gaji_id ASC" ;
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$com}&task={$task}&field={$field}&key={$key}&page=";	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$pager_url_last = null;
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 

		$datas_model_entry = array(
					'1'=>'Upload',
					'2'=>'Rumus',
					'3'=>'Tabel gol. Jabatan',
					'4'=>'Tabel Profesi',
					'5'=>'Nilai Pajak',
					'6'=>'Pot. Limit Waktu',
					'7'=>'Selisih Pembulatan',
				);
	
	while($ey = my_fetch_array($result) ){
		$i++;
		if($facebox){
			$editproperty = array(
				'href'=>'index.php?com=komponen_gaji&task=edit&jid='.$jid.'&id='.$ey['komponen_gaji_id'] ,
				'rel'=>'facebox',
				'title'=>'Edit'
			);	
		}else{
			$editproperty = array(
				'href'=>'index.php?com=komponen_gaji&task=edit&jid='.$jid.'&id='.$ey['komponen_gaji_id'] ,
				'title'=>'Edit'
			);
		}
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );

		$deleteproperty = array(
			'href'=>'javascript:;',
			'title'=>'Delete',
			'onclick'=>'confirmDelete('.$ey['komponen_gaji_id'].')',
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		if($ey['kontrak']=="yes" ){
			$kontrak=button_icon( 'check2.png' ,'') ;
		}else{
			$kontrak='';
		}
		 $jurnal=my_get_data_by_id('jurnal_gaji','jurnal_gaji_id' ,$ey['jurnal_gaji_id']);
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),    
			'nama'=>  $ey['nama_komponen_gaji'] ,     
			'Model Entri'=>$datas_model_entry[$ey['model_entry']] ,
			'Jurnal'=>  $jurnal['kode_jurnal_gaji'] .'/'.substr($jurnal['nama_jurnal_gaji'],0,25),  
			'Pajak'=>position_text_align( ($ey['komponen_pajak_id']<>0  ? button_icon( 'check2.png' ,'') :  ''), 'center'),
			'operasi'=> position_text_align( $edit_button .' '.$delete_button , 'right'),
		);
	}
	$datas = table_rows($row);	
	$paging = $kgPagerOBJ ->showPaging();
	return table_builder($header , $datas ,  8 , false ,$paging );
}

function form_komponen_gaji_create($jid,$komponen_gaji_id){

	if($komponen_gaji_id>0)
		my_set_code_js('
		function checkSubmit(){
			var t = document.komponen_gaji;
			var x = confirm(\'Yakin mengubah data\');
			if(x){
				t.submit();
			}
			return;
		}'); 
	else
		my_set_code_js('

	function checkSubmit(){
		var t = document.komponen_gaji; 
		t.submit(); 
		 
	}'); 	
	
	$fields = my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_gaji_id);
	$komponens = array();
	$query = "SELECT * FROM pr_komponen_pajak
		WHERE komponen_pajak_id < 7
			OR komponen_pajak_id = 8 
			OR komponen_pajak_id = 12";
	$result = my_query($query);
	while($row =my_fetch_array($result)){
		//if(strlen($row['label_pajak']) > 25)
		//	$komponens[$row['komponen_pajak_id']] = substr( $row['label_pajak'],0,25).' ...';
		//else
			$komponens[$row['komponen_pajak_id']] = $row['label_pajak'];
	}
	$komponen = array(
		'style'=>'width:220px',
		'name'=>'komponen_pajak_id',
		'value'=>(
			isset( $_POST['komponen_pajak_id'] )
				? $_POST['komponen_pajak_id'] 
				: $fields['komponen_pajak_id'] 
				)
		,'id'=>'komponen_pajak_id'
	);

	$form_dropdown_pajak = form_dropdown($komponen , $komponens);
/*
	$komponen2 = array(
		'style'=>'width:220px',
		'name'=>'komponen_pajak_id1',
		'value'=>(
			isset( $_POST['komponen_pajak_id1'] )
				? $_POST['komponen_pajak_id1'] 
				: $fields['komponen_pajak_id1'] 
				)
		,'id'=>'komponen_pajak_id1'
	);

	$form_dropdown_pajak .= '<br/>'.form_dropdown($komponen2 , $komponens);
*/
	$profs = array();
	$query = "SELECT * FROM profesi";
	$res = my_query($query);
	while($rw = my_fetch_array($res)){
		$profs[$rw['profesi_id']] = $rw['nama_profesi'];
	}
	$profesi  = array(
		'name'=>'profesi_id',
		'value'=> isset($_POST['profesi_id']) ? $_POST['profesi_id'] : $fields['profesi_id'],
		'id'=>'profesi_id',
	);
	$form_dropdown_profesi=form_dropdown($profesi,$profs);
	my_set_code_js('   
		$(document).ready(function() {
			$("#model_entry").change(function(){ 
				if($(this).val() == \'4\'){
					$("#pantext_model_entry").replaceWith("<span id=\'pantext_model_entry\'>&nbsp;'.addslashes(str_replace("
	","",$form_dropdown_profesi)).'</span>");
				}else{
					$("#pantext_model_entry").replaceWith("<span id=\'pantext_model_entry\'></span>");
				}
			});
		 
			$("#kena_pajak2").change(function(){ 
			 if ($(this).is(":checked") ) { 
			   $("#pantext_pajak").replaceWith("<span id=\'pantext_pajak\'></span>");
			 } 
		   }); 
		   $("#kena_pajak1").change(function(){ 
			 if ($(this).is(":checked") ) { 
			   $("#pantext_pajak").replaceWith("<span id=\'pantext_pajak\'>'.addslashes(str_replace("
	","",$form_dropdown_pajak)).'</span>");
			 } 
		   });
		});
		'
	);

	my_set_code_js('
	$(function () { // this line makes sure this code runs on page load
		$(\'.checkall\').click(function () {
			$(this).parents(\'pg:eq(0)\').find(\':checkbox\').attr(\'checked\', this.checked);
		});
	});

	function testFormula(){
		var fformula = $(\'#formula\').val();
		 $.get("check_formula.php", { formula: fformula },
			function(data){
				
				alert("Verify :" + data);
		 });
	}

	');
	$view = form_header( "komponen_gaji" , "komponen_gaji"  );
	
	$nama = array(
		'name'=>'nama_komponen_gaji',
		'id'=>'nama_komponen_gaji',
		'value'=>isset($_POST['nama_komponen_gaji']) ? $_POST['nama_komponen_gaji'] : $fields['nama_komponen_gaji'],
		'type'=>'text',
		'size'=>'50', 		
	);
	$form_nama_komponen_gaji = form_dynamic($nama);
	$view .= form_field_display( $form_nama_komponen_gaji , "Nama" );	

	if($jid==1){	
		$datas_model_entry = array(
					'1'=>'Upload',
					'2'=>'Rumus',
					'3'=>'Tabel gol. Jabatan',
					//'4'=>'Tabel Profesi',
					'5'=>'Nilai Pajak',
					'6'=>'Pot. Limit Waktu',
					'7'=>'Selisih Pembulatan',					
				);
	}elseif($jid==2){
		$datas_model_entry = array(
					'1'=>'Upload',
					'2'=>'Rumus',
					'6'=>'Pot. Limit Waktu',
					'7'=>'Selisih Pembulatan',					
				);
	}elseif($jid==3){
		$datas_model_entry = array(
					'1'=>'Upload',
					'2'=>'Rumus',
					'5'=>'Nilai Pajak',
					'6'=>'Pot. Limit Waktu',
					'7'=>'Selisih Pembulatan',					
				);
	}
	$model_entry = array(
		'name'=>'model_entry',
		'id'=>'model_entry',
		'value'=>isset($_POST['model_entry']) ? $_POST['model_entry'] : $fields['model_entry'] 
	);
	$form_model_entry = form_dropdown($model_entry , $datas_model_entry);
	$view .= form_field_display( $form_model_entry.'<span id=\'pantext_model_entry\'>'.( $fields['model_entry']=='4'? $form_dropdown_profesi : '').'</span>'  , "Model entry" );
	
	if($fields['kontrak']=='yes'){ 
		$kontrak = array(
			'name'=>'kontrak',
			'id'=>'kontrak',
			'type'=>'checkbox',
			'checked'=>'checked'
			
		);
	}else{
		$kontrak = array(
			'name'=>'kontrak',
			'id'=>'kontrak',
			'type'=>'checkbox',
		); 
	}
	$form_kontrak = form_dynamic($kontrak).'Kontrak';
	
	if($fields['organik']=='yes'){ 
		$organik = array(
			'name'=>'organik',
			'id'=>'organik',
			'type'=>'checkbox',
				'checked'=>'checked'
		);
	}else{
		$organik = array(
			'name'=>'organik',
			'id'=>'organik',
			'type'=>'checkbox'
		);
	}
	$form_organik = form_dynamic($organik).'Organik';
	
	if($fields['perbantuan']=='yes'){ 
		$perbantuan = array(
			'name'=>'perbantuan',
			'id'=>'perbantuan',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$perbantuan = array(
			'name'=>'perbantuan',
			'id'=>'perbantuan',
			 'type'=>'checkbox' 
		);
	}
	$form_perbantuan = form_dynamic($perbantuan).'Perbantuan';

	if($fields['alih_status']=='yes'){ 
		$alih_status = array(
			'name'=>'alih_status',
			'id'=>'alih_status',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$alih_status = array(
			'name'=>'alih_status',
			'id'=>'alih_status',
			 'type'=>'checkbox' 
		);
	}
	$form_alih_status = form_dynamic($alih_status).'Alih Status';	
	
	
	$form_komponen_status ='<pg>'
	.$form_kontrak.' &nbsp; '
	.$form_organik.' &nbsp; '
	.$form_perbantuan .' &nbsp; '
	.$form_alih_status .' &nbsp; 
	<input type="checkbox" class="checkall"> Check all<pg>
	';
	$view .= form_field_display( $form_komponen_status , "Status karyawan" );

	$c = array(
		'name'=>'formula',
		'value'=>( isset($_POST['formula']) ? $_POST['formula'] : $fields['formula'] ),
		'id'=>'formula',  
		'rows'=>'2',
		'size'=>'100',
		'onchange'=>'javascript:testFormula()'
	);
	$form_field_nama = form_textarea($c);
	$view .= form_field_display( $form_field_nama   , "Formula");

	$c = array(
		'name'=>'formula_normatif',
		'value'=>( isset($_POST['formula_normatif']) ? $_POST['formula_normatif'] : $fields['formula_normatif'] ),
		'id'=>'formula_normatif',  
		'rows'=>'2',
		'size'=>'100',
		'onchange'=>'javascript:testFormula()'
	);
	$form_field_nama = form_textarea($c);
//	$view .= form_field_display( $form_field_nama   , "Formula Normatif");
	
	$d = array(
		'name'=>'formula_rapel',
		'value'=>( isset($_POST['formula_rapel']) ? $_POST['formula_rapel'] : $fields['formula_rapel'] ),
		'id'=>'formula_rapel',  
		'rows'=>'2',
		'size'=>'100', 
	);
	$form_field_d = form_textarea($d);
//	$view .= form_field_display( $form_field_d   , "Formula Rapel");
	
	$kenas = array('ya'=>'Ya','tidak'=>'Tidak'); 
	$kena_pajak = array(
		'name'=>'kena_pajak',
		'id'=>'kena_pajak' ,
		'value'=>( ($fields['komponen_pajak_id'] > 0 )? "ya" :"tidak" )
	); 
	$form_kena_pajak = form_radiobutton($kena_pajak,$kenas) ;
	if( (int) $fields['komponen_pajak_id']  > 0)
		$view .= form_field_display( $form_kena_pajak .'<span id=\'pantext_pajak\'>'.$form_dropdown_pajak.'</span>' , "Perhitungan Pajak" );
	else
		$view .= form_field_display( $form_kena_pajak .'<span id=\'pantext_pajak\'></span>' , "Perhitungan Pajak" );
	
	 $pajaks = array();
	 $query_pajak = "SELECT * FROM pr_komponen_pajak 
		WHERE komponen_pajak_id < 7
		OR komponen_pajak_id = 8 
		OR komponen_pajak_id = 12";
	 $re = my_query($query_pajak);
	 while($row = my_fetch_array($re) ){
		$pajaks[$row['komponen_pajak_id']] =substr($row['label_pajak'], 0, 50);
	 }
	 $komponen_pajak_id = array(
		'name'=>'komponen_pajak_id',
		'id'=>'komponen_pajak_id',
		'value'=>isset($_POST['komponen_pajak_id']) ? $_POST['komponen_pajak_id'] : $fields['komponen_pajak_id'] ,
		'size'=>'35'
	);
	$form_model_pjk = form_dropdown($komponen_pajak_id , $pajaks); 

	 $jurnal = array();
	 $query_jurnal = "SELECT * FROM jurnal_gaji order by nama_jurnal_gaji";
	 $re = my_query($query_jurnal);
	 while($row = my_fetch_array($re) ){
		$jurnal[$row['jurnal_gaji_id']] =$row['nama_jurnal_gaji'].' ('.$row['kode_jurnal_gaji'].')';
	 }
	 $jurnal_gaji_id = array(
		'name'=>'jurnal_gaji_id',
		'id'=>'jurnal_gaji_id',
		'value'=>isset($_POST['jurnal_gaji_id']) ? $_POST['jurnal_gaji_id'] : $fields['jurnal_gaji_id'] 
	);
	$form_model_jurnal = form_dropdown($jurnal_gaji_id , $jurnal);
	$view .= form_field_display( $form_model_jurnal  , "Jurnal Gaji" ); 

	if($fields['reset_tutup_bulan']=='yes'){ 
	$reset_tutup_bulan = array(
		'name'=>'reset_tutup_bulan',
		'id'=>'reset_tutup_bulan',
		 'type'=>'checkbox',
			'checked'=>'checked'
	);
	}else{
	$reset_tutup_bulan = array(
		'name'=>'reset_tutup_bulan',
		'id'=>'reset_tutup_bulan',
		 'type'=>'checkbox' 
	);
	}
	$form_reset_tutup_bulan = form_dynamic($reset_tutup_bulan).'&nbsp;Reset data saat tutup bulan';
	$view .= form_field_display( $form_reset_tutup_bulan , "&nbsp;" );

	if($fields['remunerasi']=='Y'){ 
		$remunerasi = array(
			'name'=>'remunerasi',
			'id'=>'remunerasi',
			 'type'=>'checkbox',
				'checked'=>'checked'
		);
		}else{
		$remunerasi = array(
			'name'=>'remunerasi',
			'id'=>'remunerasi',
			 'type'=>'checkbox' 
		);
	}
	$form_remunerasi = form_dynamic($remunerasi). " Remunerasi";

	if($fields['tunjangan_jpk']=='Y'){ 
		$temp = array(
			'name'=>'tunjangan_jpk',
			'id'=>'tunjangan_jpk',
			 'type'=>'checkbox',
				'checked'=>'checked'
		);
	}else{
		$temp = array(
			'name'=>'tunjangan_jpk',
			'id'=>'tunjangan_jpk',
			 'type'=>'checkbox' 
		);
	}
	$form_temp1 = form_dynamic($temp).' Tunjangan Kesra';
	$view .= form_field_display( $form_remunerasi .' &nbsp; '.$form_temp1 , "");
	
	
	if($fields['direksi_null']=='Y'){ 
		$tempdireksi = array(
			'name'=>'direksi_null',
			'id'=>'direksi_null',
			 'type'=>'checkbox',
				'checked'=>'checked'
		);
	}else{
		$tempdireksi = array(
			'name'=>'direksi_null',
			'id'=>'direksi_null',
			 'type'=>'checkbox' 
		);
	}
	$form_tempdireksi = form_dynamic($tempdireksi).' Direksi tidak berlaku';
	$view .= form_field_display( $form_tempdireksi ,'&nbsp;' );
	
	
	$submit = array(
		'value' => (  ' Simpan '  ),
		'name' => 'simpan', 'onclick'=>'javascript:checkSubmit()',
		'type'=>'button','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit);
	
	$view .= form_field_display( $form_submit, "&nbsp;" ,  "" );
	$view .= form_footer( );	
	return $view;
}

function form_komponen_gaji_submit($jid,$komponen_gaji_id = 0){
	$kontrak = isset($_POST['kontrak']) ? 'yes': 'no';
	$organik = isset($_POST['organik']) ? 'yes': 'no';
	$perbantuan = isset($_POST['perbantuan']) ? 'yes': 'no';
	$alih_status = isset($_POST['alih_status']) ? 'yes': 'no';	
	$reset_tutup_bulan = isset($_POST['reset_tutup_bulan']) ? 'yes': 'no';
	$remunerasi = isset($_POST['remunerasi']) ? 'Y': 'T';
	$tunjangan_jpk = isset($_POST['tunjangan_jpk']) ? 'Y': 'T';
	$direksi_null = isset($_POST['direksi_null']) ? 'Y': 'T';
	$datas = array(
			'nama_komponen_gaji'=>my_type_data_str($_POST['nama_komponen_gaji']), 		 	 	 	 	 	 	 
			'tipe'	=>my_type_data_str($jid), 		 	 	 	 	 	 	
			'kontrak'	 =>my_type_data_str($kontrak),			 	 	 	 	 	 	
			'organik'	=>my_type_data_str($organik), 			 	 	 	 	 	 	
			'perbantuan'=>my_type_data_str($perbantuan),	 			 	 	 	 	 	 	
			'alih_status'=>my_type_data_str($alih_status),	 			 	 	 	 	 	 	
			'model_entry'=>my_type_data_str($_POST['model_entry']),
			'formula'=>my_type_data_str($_POST['formula']),
			'formula_normatif'=>my_type_data_str($_POST['formula_normatif']),
			'formula_rapel'=>my_type_data_str($_POST['formula_rapel']),
			'komponen_pajak_id1'=>my_type_data_int($_POST['komponen_pajak_id1']),	
			'komponen_pajak_id'=>my_type_data_int($_POST['komponen_pajak_id']),	
			'jurnal_gaji_id'=>my_type_data_int($_POST['jurnal_gaji_id']),	
			'profesi_id'=>( $_POST['model_entry'] =='4' ? my_type_data_int($_POST['profesi_id']) :my_type_data_int( '0') ),	
			'reset_tutup_bulan'=>my_type_data_str($reset_tutup_bulan),	
			'remunerasi'	=>my_type_data_str($remunerasi), 			 	 	 	 	 	 	
			'tunjangan_jpk'	=>my_type_data_str($tunjangan_jpk), 			 	 	 	 	 	 	
			'direksi_null'	=>my_type_data_str($direksi_null), 			 	 	 	 	 	 	
		);
	if($komponen_gaji_id > 0){
		return my_update_record('pr_komponen_gaji','komponen_gaji_id',$komponen_gaji_id, $datas);
	}
	return my_insert_record('pr_komponen_gaji' , $datas);
}

function form_komponen_gaji_validate($jid,$komponen_gaji_id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["nama_komponen_gaji"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Nama belum diisi";
	}
	
	$query="SELECT komponen_gaji_id FROM pr_komponen_gaji 
		WHERE nama_komponen_gaji='".$code.
		"' AND komponen_gaji_id<>".$komponen_gaji_id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Nama sudah digunakan";	
	}
  
	$code =  trim($_POST["model_entry"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] = "Model Entry belum diisi";
	}

	if( $code == '2' AND trim($_POST["formula"])==''){
		$errsubmit = true;
		$err[] = "Formula belum diisi";
	}

	if( $code == '4' AND trim($_POST["profesi_id"])=='0'){
		$errsubmit = true;
		$err[] = "Profesi belum diisi";
	}	

	$code =  trim($_POST["jurnal_gaji_id"]); 
	if( $code == '0'){
		$errsubmit = true;
		$err[] = "Jurnal gaji belum diisi";
	}
	
	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}


function delete_komponen_gaji($komponen_gaji_id){
	$query = "DELETE FROM pr_komponen_gaji WHERE komponen_gaji_id ={$komponen_gaji_id} ";
	return my_query($query);
}	
